package com.topisv.tms.utils.print;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.topisv.tms.service.vo.PackItems;
import com.topisv.tms.service.vo.PackStyleModel;
import com.topisv.tms.service.vo.PrinterModel;
import com.topisv.tms.utils.FileOperation;

public class PackFormUtil {
	private static final Logger logger = LoggerFactory.getLogger(PackFormUtil.class);
	private static String replaceParams(String content, PackItems packItem,int j)
			throws Exception {
		Class<PackItems> clazz = PackItems.class;
		Field[] fields = clazz.getDeclaredFields();
		for (int i = 0; i < fields.length; i++) {
			String paramName = fields[i].getName();
			Method method = clazz.getMethod(getMethod(paramName));
			Object object = method.invoke(packItem);
			content=content.replace("${obj.index}", j+"");
			if (object != null) {
				content = content.replace("${obj." + paramName + "}",object.toString());
			} else {
				content = content.replace("${obj." + paramName + "}", "");

			}
		}
		return content;
	}

	/**
	 * 对变量进行替换
	 * 
	 * @param content
	 * @param printerModer
	 * @return
	 * @throws Exception
	 */
	public static String replaceParams(String content, PrinterModel printerModel)
			throws Exception {
			for(Class<?> clazz = printerModel.getClass() ; clazz != Object.class ; clazz = clazz.getSuperclass()) {
				try {
	             
			Field[] fields = clazz.getDeclaredFields();
			for (int i = 0; i < fields.length; i++) {
				String paramName = fields[i].getName();
				Method method = clazz.getMethod(getMethod(paramName));
				Object object = method.invoke(printerModel);
				if("packItems".equals(paramName)){
					int firstIndex=content.indexOf("<c:forEach items=\"${packItems}\" var=\"obj\">");
					if(firstIndex<0){
						continue;
					}
					String firstContent=content.substring(0, firstIndex);
					int endIndex=content.indexOf("</c:forEach>");
					String endContent=content.substring(endIndex+12);
					String bodyContent=content.substring(firstIndex+42, endIndex);
					List<PackItems> packItems=(List<PackItems>) object;
					StringBuffer tempBody=new StringBuffer();
					int j=1;
					for(PackItems obj:packItems){
						String indexContent=replaceParams(bodyContent,obj,j);
						tempBody.append(indexContent);
						j++;
					}
					content=firstContent+tempBody.toString()+endContent;
					continue;
				}
				if (object != null) {
					content = content.replace("${" + paramName + "}",object.toString());
				} else {
					content = content.replace("${" + paramName + "}", "");

				}
			}
		} catch (Exception e) {
			logger.error("error",e);
			throw e;
		}
		}
		return content;
	}

	

	/**
	 * 对变量进行替换
	 * 
	 * @param content
	 * @param printerModer
	 * @return
	 * @throws Exception
	 */
	public static String replaceParams(String content, PackStyleModel packStyleModel)
			throws Exception {
		try {
			content=FileOperation.readerStyleContent(content);
			Class<PackStyleModel> clazz = PackStyleModel.class;
			Field[] fields = clazz.getDeclaredFields();
			for (int i = 0; i < fields.length; i++) {
				String paramName = fields[i].getName();
				Method method = clazz.getMethod(getMethod(paramName));
				Object object = method.invoke(packStyleModel);
				if("packItems".equals(paramName)){
					int firstIndex=content.indexOf("<c:forEach items=\"${packItems}\" var=\"obj\">");
					String firstContent=content.substring(0, firstIndex);
					int endIndex=content.indexOf("</c:forEach>");
					String endContent=content.substring(endIndex+12);
					String bodyContent=content.substring(firstIndex+42, endIndex);
					List<PackItems> packItems=(List<PackItems>) object;
					StringBuffer tempBody=new StringBuffer();
					int j=1;
					for(PackItems obj:packItems){
						String indexContent=replaceParams(bodyContent,obj,j);
						tempBody.append(indexContent);
						j++;
					}
					content=firstContent+tempBody.toString()+endContent;
					continue;
				}
				if (object != null) {
					content = content.replace("${" + paramName + "}",object.toString());
				} else {
					content = content.replace("${" + paramName + "}", "");

				}
			}
		} catch (Exception e) {
			logger.error("error",e);
			throw e;
		}
		return content;
	}

	private static String getMethod(String param) {
		StringBuffer sb = new StringBuffer(param.subSequence(0, 1));
		String result = "get" + sb.toString().toUpperCase()
				+ param.substring(1);
		return result;
	}

	public static void main(String[] args) throws Exception {
		String temp="<c:forEach items=\"${packItems}\" var=\"obj\"><tr><td>${obj.itemName}</td></tr></c:forEach>";
		PackStyleModel model = new PackStyleModel();
		List<PackItems> packItems=new ArrayList<PackItems>();
		PackItems obj1=new PackItems();
		obj1.setItemName("sss");
		packItems.add(obj1);
		PackItems obj2=new PackItems();
		obj2.setItemName("sss");
		packItems.add(obj2);
		PackItems obj3=new PackItems();
		obj3.setItemName("sss");
		packItems.add(obj3);
		model.setAllTotal("111");
		model.setPackItems(packItems);
	}
}

